home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / slideshow / sources (complete) / rolloverbutton.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  2.7 KB  |  113 lines

  1. /**
  2.  * An abstract class to implement the functionality associated with
  3.  * a "roll over button."  The basic behavior is such that when the mouse
  4.  * is "rolled over" the button, the button reacts by displaying a
  5.  * different image to give instant feedback to the user.
  6.  */
  7. public abstract class RolloverButton extends ImageButton
  8. {
  9.     //Declare data members
  10.     //Insert "RolloverButton data members"
  11.     protected String upImage;
  12.     protected String downImage;
  13.     protected String rolloverImage;
  14.     
  15.     /**
  16.      * Constructs a default instance of this class.
  17.      */
  18.     public RolloverButton()
  19.     {
  20.         //Initialize the state of the button
  21.         //Insert "RolloverButton init state"
  22.         upImage = "up";
  23.         downImage = "down";
  24.         rolloverImage = "rollover";
  25.         initImages();
  26.         setImage(upImage);
  27.     }
  28.     
  29.     /**
  30.      * Sub classes need to define this to handle initializing their
  31.      * images, and state information.
  32.      */
  33.     protected abstract void initImages();
  34.     
  35.     /**
  36.      * Sets the button to be in the correct configuration for the
  37.      * current state.
  38.      */
  39.     public void refreshImage()
  40.     {
  41.         //Handle determining the current state, and reacting appropriately
  42.         //Insert "RolloverButton refreshImage"
  43.         if (isMouseInside)
  44.         {
  45.             if (isMouseDown)
  46.             {
  47.                 setImage(downImage);
  48.             }
  49.             else
  50.             {
  51.                 setImage(rolloverImage);
  52.             }
  53.         }
  54.         else
  55.         {
  56.             setImage(upImage);
  57.         }
  58.     }
  59.     
  60.     /**
  61.      * Gets called when the mouse button is pressed on this button.
  62.      */
  63.     protected void handleMousePressed()
  64.     {
  65.         //Set the image to the appropriate image for a mouse press.
  66.         //Insert "RolloverButton mousePressed"
  67.         setImage(downImage);
  68.     }
  69.  
  70.     /**
  71.      * Gets called when the mouse button is released on this button.
  72.      * @param isMouseInside, if true, the mouse is located inside the button area,
  73.      * if false the mouse is outside the button area.
  74.      */
  75.     protected void handleMouseRelease(boolean isMouseInside)
  76.     {
  77.         //Set the image to the appropriate image for a mouse release,
  78.         //and calls the super classes version to include inherited functionality.
  79.         //Insert "RolloverButton mouseReleased"
  80.         if (isMouseInside)
  81.         {
  82.             setImage(rolloverImage);
  83.         }
  84.         super.handleMouseRelease(isMouseInside);
  85.     }
  86.  
  87.     /**
  88.      * Gets called when the mouse crosses into or out of the button area.
  89.      * @param isMouseInside, is true if the mouse is in the button area,
  90.      * false if it is outside.
  91.      * @param isMouseDown, is true if the mouse button is pressed, false if it is not.
  92.      */
  93.     protected void handleRollover(boolean isMouseInside, boolean isMouseDown)
  94.     {
  95.         //Handle determining the current state, and reacting appropriately
  96.         //Insert "RolloverButton handleRollover"
  97.         if (isMouseInside)
  98.         {
  99.             if (isMouseDown)
  100.             {
  101.                 setImage(downImage);
  102.             }
  103.             else
  104.             {
  105.                 setImage(rolloverImage);
  106.             }
  107.         }
  108.         else
  109.         {
  110.             setImage(upImage);
  111.         }
  112.     }
  113. }